package javdoku;

public class SolutionCheck {
	private int[][] grid;
	private int[][] input;
	
	public SolutionCheck(){
		
	}
	
	private boolean isPlaceGood(int r, int c, int v){
		if (input[r][c]!=0 && input[r][c]==v)
			return true;
		if (v==0)
			return true;
        for (int i=0; i<9; i++){
            if (i!=c && grid[r][i]==v){
            	return false;
            }
        }
        for (int i=0; i<9; i++){
            if (i!=r && grid[i][c]==v)
                return false;
        }
        for (int i=(r-(r%3)); i<(r-(r%3)+3); i++)
        	for (int j=(c-(c%3)); j<(c-(c%3)+3); j++){
        		if (i!=r && j!=c && grid[i][j]==v){
        			return false;
        		}
        	}
        return true;
    }
	private void setPlace(int row, int col, int val){
        grid[row][col]=val;
    }
	public boolean checkBoard(int[][] in){
		input = in;
        grid = new int[9][9];
        for (int i=0; i<9; i++){
            for (int j=0; j<9; j++)
                grid[i][j]=0;
        }
        int num=0;
        int row=0;
        int col=0;
        int val=1;
        while ((row < 9) && (col < 9) && (row>-1)){
        	if (row==8 && col==8){
        		grid[row][col]=input[row][col];
        	}
        	while (input[row][col]!=0 && (row!=8 || col!=8)){
        		grid[row][col]=input[row][col];
        		if (col<8)
        			col++;
        		else if (row<8){
        			col=0;
        			row++;
        		}
        	}
            //self._printBoard()
            while (!isPlaceGood(row, col, val) && (row>-1)){
                //printBoard(); 
                if (val>=9){
                	if (row!=8 && col!=8)
                		do {                		
                			setPlace(row, col, input[row][col]);
                			col--;
                			if (col<0 && row>0){
                				row--;
                				col=8;
                			}
                		} while(row>=0 && col>=0 && input[row][col]!=0);
                	if (row<=0 && col<=0){
                		row=0;
                		col=0;
                	}
            		val=grid[row][col]+1;
                    /*if self.col > 0:
                        self.col-=1
                        self.val = self.grid[self.row][self.col]+1
                    elif self.col == 0 and self.row > 0:
                        self.col=8
                        self.row-=1
                        self.val = self.grid[self.row][self.col]+1
                    else:
                        self.row=9*/
                }
                
                if (val<9)
                    val++;
            }
            setPlace(row, col, val);
            System.out.println(row);
            System.out.println(col);
            System.out.println();
            //print(self.row, self.col, self.val)
            if (col == 8 && row == 8){
                //made board
            	if (num==1)
            		return false;
                num++;
                //writeBoard(file, num);
                val++;
                //if (num%1000==0)
                    //System.out.println(num);
            }
            else if (col < 8){
                col++;
                val=1;
            }
            else if (row < 8){
                col=0;
                row++;
                val=1;
            }
        }
        return true;
     }
}
